#!/usr/bin/python3

"""
@author: 张伟
@time: 2018/4/19 10:10
"""
from selenium import webdriver
import time

web_driver_path = r'./chromedriver.exe'


def init():
    w = open(file=r'./singer/singer.txt', mode='a', encoding='utf-8', newline='\n')
    browser = webdriver.Chrome(web_driver_path)
    for i in range(1, 5527):
        path = 'https://y.qq.com/portal/singer_list.html#t4=' + str(i) + '&t3=all&t2=all&t1=all&'
        browser.get(path)
        browser.refresh()
        time.sleep(2)
        browser.refresh()
        time.sleep(2)
        input_third = browser.find_elements_by_class_name('singer_list_txt__item')
        for s in input_third:
            name = s.text
            url = s.find_element_by_tag_name('a').get_attribute('href')
            s = url + "\t" + name + '\n'
            w.write(s)
            w.flush()
            print(s, end='')
    browser.close()
    w.close()


def load_single_singer(singer_name):
    ls = list()
    r = open(file=r'./singer/singer.txt', mode='r', encoding='utf-8')
    url = None
    for l in r.readlines():
        if singer_name in l:
            url = l.split('\t')[0]
            break
    r.close()
    if url:
        w = open(file=r'./' + singer_name + '-song.txt', mode='w', encoding='utf-8', newline='\n')
        browser = webdriver.Chrome(web_driver_path)
        browser.get(url=url)
        browser.refresh()
        time.sleep(5)
        input_third = browser.find_elements_by_class_name('songlist__songname_txt')
        for s in input_third:
            song = s.text
            url = s.find_element_by_tag_name('a').get_attribute('href')
            s = url + "\t" + song + '\n'
            if url and song:
                w.write(s)
                w.flush()
                ls.append(url)
                print(s, end='')
        while True:
            try:
                browser.find_element_by_css_selector('[class="next js_pageindex"]').click()
                time.sleep(2)
                inputs = browser.find_elements_by_class_name('songlist__songname_txt')
                for s in inputs:
                    song = s.text
                    url = s.find_element_by_tag_name('a').get_attribute('href')
                    s = url + "\t" + song + '\n'
                    if url and song:
                        w.write(s)
                        w.flush()
                        ls.append(url)
                        print(s, end='')
            except:
                break
        browser.close()
        w.close()
    return ls


def load_words_song(singer_name):
    ls = load_single_singer(singer_name=singer_name)
    w = open(file=r'./' + singer_name + '-words.txt', mode='a', encoding='utf-8', newline='\n')
    browser = webdriver.Chrome(web_driver_path)
    for url in ls:
        browser.get(url=url)
        browser.refresh()
        time.sleep(3)
        b = browser.find_elements_by_class_name('lyric__cont_box')
        try:
            browser.find_element_by_css_selector('[class="c_tx_highlight js_open_lyric"]').click()
        except:
            pass
        time.sleep(2)
        for p in b:
            t = p.text
            w.write(t)
            w.write('\n#########################' + '\n')
            w.flush()
            print(t)
    browser.close()
    w.close()
